﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EC_DataBase;

namespace EasyCheque.Welcome
{
    public partial class RegisterLoginAccount : System.Web.UI.Page
    {
        private string m_str_Male = "男";
        private string m_str_Female = "女";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            // 处理生日控件。
            int year = 1900, month = 1, day = 1;
            while (year <= 2010)
            {
                DropDownList_BIRTHDAY_YEAR.Items.Add(year.ToString());
                year++;
            }

            while (month <= 12)
            {
                DropDownList_BIRTHDAY_MONTH.Items.Add(month.ToString());
                month++;
            }

            while (day <= 31)
            {
                DropDownList_BIRTHDAY_DAY.Items.Add(day.ToString());
                day++;
            }

            // 界面处理。
            DropDownList_SEX.Items.Add(m_str_Male);
            DropDownList_SEX.Items.Add(m_str_Female);

            Label_CommitMessage.Visible = false;
        }

        protected void DropDownList_BIRTHDAY_MONTH_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList_BIRTHDAY_DAY.Items.Clear();

            int selectedYear = Convert.ToInt32(DropDownList_BIRTHDAY_YEAR.SelectedValue);
            int selectedMonth = Convert.ToInt32(DropDownList_BIRTHDAY_MONTH.SelectedValue);
            int day = 1;
            while (day <= DateTime.DaysInMonth(selectedYear, selectedMonth))
            {
                DropDownList_BIRTHDAY_DAY.Items.Add(day.ToString());
                day++;
            }
        }

        protected void Button_Register_Click(object sender, EventArgs e)
        {
            // 先判断新用户的身份证是否存在。
            if (OperateDB.HasRows(OperateDB.TN_PERSON_INFO, "IDCARD", TextBox_IDCARD.Text))
            {
                System.Diagnostics.Debug.WriteLine("Already have one.");
                Label_CommitMessage.Visible = true;
                Label_CommitMessage.Text = "该身份证已注册。";
                return;
            }

            if (OperateDB.HasRows(OperateDB.TN_PERSON_ACCOUNT, "ACCOUNT_ID", TextBox_ACCOUNT_ID.Text))
            {
                System.Diagnostics.Debug.WriteLine("Already have one.");
                Label_CommitMessage.Visible = true;
                Label_CommitMessage.Text = "该银行帐户已注册。";
                return;
            }

            if (!OperateDB.HasRows(OperateDB.TN_ACCOUNT_INFO, "ACCOUNT_ID", TextBox_ACCOUNT_ID.Text))
            {
                System.Diagnostics.Debug.WriteLine("None account ID have one.");
                Label_CommitMessage.Visible = true;
                Label_CommitMessage.Text = "银行帐户不存在。";
                return;
            }

            // 2010-09-06 Wander added.
            // 检测密码正确性。
            if (!Security.MD5.Exam(TextBox_ACCOUNT_PASSWORD.Text, System.Text.Encoding.ASCII, OperateDB.Get_AccountPwd(TextBox_ACCOUNT_ID.Text)))
            {
                // 检测到密码无效。
                System.Diagnostics.Debug.WriteLine("Password invalid.");
                Label_CommitMessage.Text = "银行帐户密码错误。";
                return;
            }
            else
            {
                Label_CommitMessage.Text = "";
            }


            // 开始往数据库中加人。
            // 写表TN_PERSON_INFO。
            string secCode = Security.MD5.HexCompute(DateTime.Now.ToString(), System.Text.Encoding.ASCII);
            string[] valuesToInsert = new string[]
                {
                    TextBox_NAME.Text, 
                    DropDownList_SEX.SelectedValue, 
                    TextBox_ENAME.Text, 
                    DropDownList_BIRTHDAY_YEAR.SelectedValue+"-"+DropDownList_BIRTHDAY_MONTH.SelectedValue+"-"+DropDownList_BIRTHDAY_DAY.SelectedValue, 
                    TextBox_IDCARD.Text, 
                    TextBox_ADDRESS.Text, 
                    TextBox_PHONE.Text, 
                    TextBox_EMAIL.Text, 
                    Security.MD5.HexCompute(TextBox_PASSWORD.Text, System.Text.Encoding.ASCII), 
                    TextBox_ACCOUNT.Text, 
                    DateTime.Now.ToString(), 
                    TextBox_CELLPHONE.Text, 
                    "True", 
                    secCode
                };
            AccessDB.Insert(OperateDB.TN_PERSON_INFO, valuesToInsert);

            // 写表TN_PERSON_ACCOUNT。
            int uid = OperateDB.GetUID_By_IDCard(TextBox_IDCARD.Text);
            //string[] valuesForPA = new string[]
            //    {
            //        uid.ToString(), 
            //        TextBox_ACCOUNT_ID.Text
            //    };
            //AccessDB.Insert(OperateDB.TN_PERSON_ACCOUNT, valuesForPA);
            AccessDB.ExectueSql("INSERT INTO " + OperateDB.TN_PERSON_ACCOUNT + " VALUES (" + uid.ToString() + ", '" + TextBox_ACCOUNT_ID.Text + "') ");
            AccessDB.ExectueSql("UPDATE " + OperateDB.TN_ACCOUNT_INFO + " SET PERSON = " + uid.ToString() + " WHERE ACCOUNT_ID = '" + TextBox_ACCOUNT_ID.Text + "' ");

            // 转移。            
            Session["RegIDCARD"] = TextBox_IDCARD.Text;
            Session["secCode"] = secCode;
            System.Diagnostics.Debug.WriteLine("In Regi RegIDCARD SESSION: " + Session["RegIDCARD"]);
            System.Diagnostics.Debug.WriteLine("In Regi registerSucceed SESSION: " + Session["registerSucceed"]);
            // ~

            // 加入成功，跳转页面。
            Response.Redirect("~/Welcome/RegisterComplete.aspx");
        }
    }
}
